package com.haiheng.module_main.fragment

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import com.haiheng.common.constant.AppConstant
import com.haiheng.common.constant.RourePage
import com.haiheng.common.fragment.RouterFragment
import com.haiheng.library.log.LogUtils
import com.haiheng.library_base.dialog.LoadingDialog
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.toast.ToastUtils
import com.haiheng.library_base.utils.SPUtils
import com.haiheng.library_base.utils.SystemUtil
import com.haiheng.module_main.BuildConfig
import com.haiheng.module_main.R
import com.haiheng.module_main.databinding.FragmentLoginBinding
import com.haiheng.module_main.mode.LoginViewModel
import com.haiheng.module_main.mode.UserViewModel

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/8/3 0003
 * @Version: 1.0
 */
class LoginFragment : RouterFragment() {
    private lateinit var binding:FragmentLoginBinding
    private val viewModel: LoginViewModel by viewModels()
    private lateinit var loadingDialog : LoadingDialog
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentLoginBinding.inflate(inflater,container,false)
        return binding.root
    }
    override fun initView() {
        loadingDialog = LoadingDialog(requireContext(), R.style.AvLoadingDialogStyle)
        binding.tvVersion.setText("书车APP ${SystemUtil.getVersionName()}")
        binding.ivClearName.setOnClickListener {
            binding.etAccount.setText("")
        }
        binding.sllLogin.setOnClickListener {
            var account = binding.etAccount.text.trim().toString()
            var passwor = binding.etPassword.text.trim().toString()
            if (TextUtils.isEmpty(account)){
                ToastUtils.showShort(R.string.text_login_account_no_empty)
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(passwor)){
                Toast.makeText(activity,"密码不能为空",Toast.LENGTH_SHORT).show()
                ToastUtils.showShort(R.string.text_login_password_no_empty)
                return@setOnClickListener
            }
            if (binding.cbPassword.isChecked){
                SPUtils.getInstance().put(AppConstant.SP_ACCOUNT,binding.etAccount.text.trim().toString())
                SPUtils.getInstance().put(AppConstant.SP_PASSWORD,binding.etPassword.text.trim().toString())
            }
            SPUtils.getInstance().put(AppConstant.SP_IS_SAVE_PASSWORD,binding.cbPassword.isChecked)
            loadingDialog.setMessage("登录中")
            loadingDialog.show()
            viewModel.login(account,passwor)
//            activity.navigation(RourePage.BOOK_CAR_LOGIN_RESULT)
        }
        binding.tvRememberPassword.setOnClickListener {
            binding.cbPassword.isChecked = !binding.cbPassword.isChecked
        }
        binding.ivSetting.setOnClickListener {
            activity.navigation(RourePage.SETTING_PAGE)
        }

    }

    override fun initViewModel(): BaseViewModel? {
        viewModel.userInfoBean.observe(viewLifecycleOwner){
            loadingDialog.hide()

//            if (it.isSuccess == true){
//                activity.navigation(RourePage.MAIN,null)
//            }else{
//                ToastUtils.showShort("${it.message}")
//            }

        }
        viewModel.isLoginFinish.observe(viewLifecycleOwner){
            loadingDialog.hide()
            if (it){
                activity.navigation(RourePage.BOOK_CAR_LOGIN_RESULT)
            }
        }
        viewModel.isFinishSyn.observe(viewLifecycleOwner){
            if (it == true){
                activity.navigation(RourePage.MAIN,null)
            }else{
                ToastUtils.showShort("同步失败")
            }
        }
        return viewModel
    }

    override fun initLoad() {
        var isRemeberPassword = SPUtils.getInstance().getBoolean(AppConstant.SP_IS_SAVE_PASSWORD,true)
        LogUtils.e("isRemeberPassword：${isRemeberPassword}")
        binding.cbPassword.isChecked = isRemeberPassword
        if (isRemeberPassword){
           var account =  SPUtils.getInstance().getString(AppConstant.SP_ACCOUNT)
           var password =  SPUtils.getInstance().getString(AppConstant.SP_PASSWORD)
           if (!TextUtils.isEmpty(account)){
                binding.etAccount.setText(account)
           }
           if (!TextUtils.isEmpty(password)){
                binding.etPassword.setText(password)
           }
        }

    }


}